package com.ds.admin.console.service;

import cn.hutool.core.lang.Assert;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ds.admin.console.entity.SysUser;
import com.ds.admin.console.mapper.SysUserMapper;
import com.ds.admin.console.param.SysUserParam;
import com.ds.admin.console.vo.SysUserVo;
import com.ds.admin.id.WorkerIdHolder;
import com.ds.common.cache.redissonLock.RedissonLock;
import com.ds.common.exception.DaoErrorEnum;
import com.ds.common.exception.SystemException;
import com.ds.common.id.BusinessCode;
import com.ds.common.id.DistributeID;
import com.ds.common.util.BeanUtil;
import com.ds.common.util.MapstructUtils;
import com.ds.common.web.request.PageParam;
import com.ds.common.web.response.PageBuilder;
import com.ds.common.web.response.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class SysUserService {

    private final SysUserMapper sysUserMapper;

    public String insert(SysUserParam param) {
        //SysUser entity = BeanUtil.copyProperties(param, SysUser.class);
        SysUser entity = MapstructUtils.convert(param, SysUser.class);
        entity.setUserId(DistributeID.generateWithSnowflake(BusinessCode.USER, WorkerIdHolder.WORKER_ID, ""));

        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysUser::getUserName, param.getUserName());
        List<SysUser> sysUsers = sysUserMapper.selectList(wrapper);
        int count = sysUserMapper.insert(entity);
        Assert.isTrue(count > 0, () -> new SystemException(DaoErrorEnum.INSERT_FAILED));
        return entity.getUserId();
    }

    public Long update(SysUserParam param) {
        SysUser entity = MapstructUtils.convert(param, SysUser.class);
        int count = sysUserMapper.updateById(entity);
        Assert.isTrue(count > 0, () -> new SystemException(DaoErrorEnum.UPDATE_FAILED));
        return entity.getId();
    }

    public Long delete(Long id) {
        int count = sysUserMapper.deleteById(id);
        Assert.isTrue(count > 0, () -> new SystemException(DaoErrorEnum.DELETE_FAILED));
        return id;
    }

    @RedissonLock(key = "#id")
    @Cached(name = "user:", key = "#id", cacheType = CacheType.BOTH, expire = 300)
    public SysUserVo select(Long id) {
        SysUser entity = sysUserMapper.selectById(id);
        //return MapstructUtils.convert(entity, SysUserVo.class);
        return BeanUtil.copyProperties(entity, SysUserVo.class);
    }

    public PageInfo<SysUserVo> selectPage(PageParam<SysUserParam> param) {
        Page<SysUserVo> info = PageHelper.startPage(param.getPageNum(), param.getPageSize())
                                         .doSelectPage(() -> sysUserMapper.selects(param.getParam()));
        return PageBuilder.build(info, info.getPages(), info.getTotal(), info.getPages());
    }
}